<template>
  <el-upload
    class="avatar-uploader el-upload--text"
    :action = "ossUrl"
    :data="dataObj"
    :show-file-list="true"
    :on-preview="videoHandlePreview"
    :on-remove="videoHandleRemove"
    :file-list="videoFileList"
    accept="file/*"
    :on-success="handleVideoSuccess"
    :before-upload="beforeUploadPicVideo"
    ref="upload"
  >
    <i class="el-icon-plus avatar-uploader-icon"></i>
    <el-progress
      v-if="videoFlag == true"
      type="circle"
      :percentage="videoUploadPercent"
      style="margin-top: 30px"
    ></el-progress>
    <el-button
      class="video-btn"
      slot="trigger"
      size="small"
      v-if="isShowUploadVideo"
      type="primary"
      >选取文件
    </el-button>
  </el-upload>
</template>
<script>
import { policy } from "./policy";
import { getUUID } from "@/utils";

export default {
  name: "singleUpload",
  props: {
    value: String,
  },
  computed: {
    videoName() {
      return this.value;
    },
    videoName() {
      if (this.value != null && this.value !== "") {
        return this.value.substr(this.value.lastIndexOf("/") + 1);
      } else {
        return null;
      }
    },
    videoFileList() {
      return [
        {
          name: this.videoName,
          url: this.videoUrl,
        },
      ];
    },
    showFileList: {
      get: function () {
        return (
          this.value !== null && this.value !== "" && this.value !== undefined
        );
      },
      set: function (newValue) {},
    },
  },
  data() {
    return {
      ossUrl: "http://renrenfilm.oss-cn-hangzhou.aliyuncs.com",
      isShowUploadVideo: false,
      videoFlag: false,
      dataObj: {
        policy: "",
        signature: "",
        key: "",
        ossaccessKeyId: "",
        dir: "",
        host: "",
        // callback:'',
      },
      dialogVisible: false,
    };
  },
  methods: {
    emitInput(val) {
      this.$emit("input", val);
    },
    videoHandleRemove(file, videoFileList) {
      this.emitInput("");
    },
    videoHandlePreview(file) {
      this.dialogVisible = true;
    },
    beforeUploadPicVideo(file) {
      let _self = this;
      return new Promise((resolve, reject) => {
        policy()
          .then((response) => {
            _self.dataObj.policy = response.data.policy;
            _self.dataObj.signature = response.data.signature;
            _self.dataObj.ossaccessKeyId = response.data.accessId;
            _self.dataObj.key = response.data.dir + getUUID() + "_${filename}";
            _self.dataObj.dir = response.data.dir;
            _self.dataObj.host = response.data.host;
            // _self.dataObj.callback = response.data.callback;
            console.log("响应的数据", _self.dataObj);
            resolve(true);
          })
          .catch((err) => {
            reject(false);
          });
      });
    },
    handleVideoSuccess(res, file) {
      console.log("上传成功...");
      this.showFileList = true;
      this.videoFileList.pop();
      this.videoFileList.push({
        name: file.name,
        url:
          this.dataObj.host +
          "/" +
          this.dataObj.key.replace("${filename}", file.name),
      });
      console.log("文件上传返回地址:", this.videoFileList[0].url);
      this.emitInput(this.videoFileList[0].url);
    },
  },
};
</script>
<style>
</style>


